//
//  WJTimeBasicController.m
//  JerryIos
//
//  Created by 王军 on 2018/2/13.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJTimeBasicController.h"
#import "NSDate+DateTools.h"


@interface WJTimeBasicController ()

@end

@implementation WJTimeBasicController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


- (IBAction)button01:(id)sender {
    NSDate *timeAgoDate = [NSDate dateWithTimeIntervalSinceNow:-4];
    NSLog(@"Time Ago: %@", timeAgoDate.timeAgoSinceNow);
    NSLog(@"Time Ago: %@", timeAgoDate.shortTimeAgoSinceNow);
    
    //输出:
    //Time Ago: 4 seconds ago
    //Time Ago: 4s
    
    //设置好支持中文输出:
    //Time Ago: 4秒钟前
    //Time Ago: 4秒
}

- (IBAction)button02:(id)sender {
    //使用中国农历
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
    
    NSDate *date = [NSDate date];
    NSInteger year = [date yearWithCalendar:calendar];
    NSInteger month = [date monthWithCalendar:calendar];
    NSInteger day = [date dayWithCalendar:calendar];
    NSInteger hour = [date hourWithCalendar:calendar];
    NSInteger minute = [date minuteWithCalendar:calendar];
    NSInteger second = [date secondWithCalendar:calendar];
    
    NSLog(@"农历: %ld-%ld-%ld %ld:%ld:%ld", year, month, day, hour, minute, second);
    NSLog(@"公历: %ld-%ld-%ld %ld:%ld:%ld", date.year, date.month, date.day, date.hour, date.minute, date.second);
    //输出: 农历: 33-4-17 11:41:51
    //输出: 公历: 2016-5-23 11:41:51
}

- (IBAction)button03:(id)sender {
    //日期增加1年
    NSDate *date = [NSDate date];
    NSDate *newDate = [date dateByAddingYears:1];
    
    NSLog(@"year:%ld newYear:%ld", date.year, newDate.year);
    //输出: year:2016 newYear:2017
}

- (IBAction)button04:(id)sender {
    NSDate *nowDate = [NSDate date];
    NSDate *beforeDate = [nowDate dateBySubtractingYears:1];
    
    if ([nowDate isEarlierThan:beforeDate]) {
        NSLog(@"earlier");
    } else {
        NSLog(@"later");
    }
}

- (IBAction)button05:(id)sender {
    /**
     *  formattedDateWithStyle(系统格式)
     */
    NSDate *date = [NSDate date];
    NSString *dateStringNo = [date formattedDateWithStyle:NSDateFormatterNoStyle];
    NSString *dateStringShort = [date formattedDateWithStyle:NSDateFormatterShortStyle];
    NSString *dateStringMedium = [date formattedDateWithStyle:NSDateFormatterMediumStyle];
    NSString *dateStringLong = [date formattedDateWithStyle:NSDateFormatterLongStyle];
    NSString *dateStringFull = [date formattedDateWithStyle:NSDateFormatterFullStyle];
    
    NSLog(@"No: %@", dateStringNo);               //输出:   No:
    NSLog(@"Short: %@", dateStringShort);         //输出:   Short: 16/5/23
    NSLog(@"Medium: %@", dateStringMedium);       //输出:   Medium: 2016年5月23日
    NSLog(@"Long: %@", dateStringLong);           //输出:   Long: 2016年5月23日
    NSLog(@"Full: %@", dateStringFull);           //输出:   Full: 2016年5月23日 星期一
    
    /**
     *  formattedDateWithFormat(自定义格式)
     */
    NSString *dateStringCustom1 = [date formattedDateWithFormat:@"yyyy-MM-dd"];
    NSString *dateStringCustom2 = [date formattedDateWithFormat:@"yyyy年MM月dd日"];
    NSString *dateStringCustom3 = [date formattedDateWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStringCustom4 = [date formattedDateWithFormat:@"yyyy年MM月dd日 HH:mm:ss"];
    NSString *dateStringCustom5 = [date formattedDateWithFormat:@"yyyy年MM月dd日 HH点mm分ss秒"];
    
    NSLog(@"Custom1: %@", dateStringCustom1);       //输出:   Custom1: 2016-05-23
    NSLog(@"Custom2: %@", dateStringCustom2);       //输出:   Custom2: 2016年05月23日
    NSLog(@"Custom3: %@", dateStringCustom3);       //输出:   Custom3: 2016-05-23 13:42:53
    NSLog(@"Custom4: %@", dateStringCustom4);       //输出:   Custom4: 2016年05月23日 13:42:53
    NSLog(@"Custom5: %@", dateStringCustom5);       //输出:   Custom5: 2016年05月23日 13点42分53秒
    
}


@end
